{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Word Ordering in a Different Alphabetical Order\n",
    "\n",
    "[原题](https://mp.weixin.qq.com/s/JG41S_GWvvMt4fPyrBhjsA)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Question\n",
    "\n",
    "Given a list of words, and an arbitrary alphabetical order, verify that the words are in order of the alphabetical order."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Example\n",
    "\n",
    "```text\n",
    "Input:\n",
    "words = [\"abcd\", \"efgh\"], order=\"zyxwvutsrqponmlkjihgfedcba\"\n",
    "\n",
    "Output: False\n",
    "```\n",
    "\n",
    "Explanation: 'e' comes before 'a' so 'efgh' should come before 'abcd'.\n",
    "\n",
    "```text\n",
    "Input:\n",
    "words = [\"zyx\", \"zyxw\", \"zyxwy\"],\n",
    "order=\"zyxwvutsrqponmlkjihgfedcba\"\n",
    "\n",
    "Output: True\n",
    "```\n",
    "\n",
    "Explanation: The words are in increasing alphabetical order."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {},
   "outputs": [],
   "source": [
    "''' Main Algorithm '''\n",
    "def isSorted(words: list, order: str):\n",
    "    k = 0\n",
    "    while k < len(words[0]) and k < len(words[1]):\n",
    "        if order.index(words[0][k]) > order.index(words[1][k]):\n",
    "            return False\n",
    "        elif words[0][k] == words[1][k]:\n",
    "            k += 1\n",
    "        else:\n",
    "            return True\n",
    "    if len(words[0]) < len(words[1]):\n",
    "        return True\n",
    "    else:\n",
    "        return False"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "False\n"
     ]
    }
   ],
   "source": [
    "''' Main Scripts: A '''\n",
    "print(isSorted([\"abcd\", \"efgh\"], \"zyxwvutsrqponmlkjihgfedcba\"))\n",
    "# False"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "True\n"
     ]
    }
   ],
   "source": [
    "''' Main Scripts: B '''\n",
    "print(isSorted([\"zyx\", \"zyxw\", \"zyxwy\"],\"zyxwvutsrqponmlkjihgfedcba\"))\n",
    "# True"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.3"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}
